package com.lv.LvTourismOrder.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aqgj.util.JsonWrapper;
import com.aqgj.util.Pager;
import com.lv.LvPriceComboOfTourismForModify.bean.LvPriceComboOfTourismForModify;
import com.lv.LvPriceComboOfTourismForModify.dao.LvPriceComboOfTourismForModifyDao;
import com.lv.LvPriceComboOfTourismModel.bean.LvPriceComboOfTourismModel;
import com.lv.LvPriceComboOfTourismModel.dao.LvPriceComboOfTourismModelDao;
import com.lv.LvRefundRecord.bean.LvRefundRecord;
import com.lv.LvRefundRecord.dao.LvRefundRecordDao;
import com.lv.LvTourismForSale.bean.LvTourismForSale;
import com.lv.LvTourismForSale.dao.LvTourismForSaleDao;
import com.lv.LvTourismForSale.service.LvTourismForSaleManager;
import com.lv.LvTourismModel.bean.LvTourismModel;
import com.lv.LvTourismModel.dao.LvTourismModelDao;
import com.lv.LvTourismOrder.bean.LvTourismOrder;
import com.lv.LvTourismOrder.dao.LvTourismOrderDao;
import com.lv.LvTourismOrderCustomer.bean.LvTourismOrderCustomer;
import com.lv.LvTourismOrderCustomer.dao.LvTourismOrderCustomerDao;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcWallet.bean.ZtcWallet;
import com.ztc.ZtcWallet.dao.ZtcWalletDao;
import com.ztc.ZtcWalletLog.bean.ZtcWalletLog;
import com.ztc.ZtcWalletLog.dao.ZtcWalletLogDao;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("LvTourismOrder")
public class LvTourismOrderManager{
    @Autowired
    LvTourismOrderDao lvTourismOrderDao;
    @Autowired
    ZtcWalletDao ztcWalletDao;
    @Autowired
    ZtcWalletLogDao ztcWalletLogDao;
    @Autowired
    LvRefundRecordDao lvRefundRecordDao;
    @Autowired
    LvTourismOrderCustomerDao lvTourismOrderCustomerDao;
    @Autowired
    LvTourismModelDao lvTourismModelDao;
    @Autowired
    LvTourismForSaleDao lvTourismForSaleDao;
    @Autowired
    LvTourismForSaleManager lvTourismForSaleManager;
    @Autowired
    LvPriceComboOfTourismModelDao lvPriceComboOfTourismModelDao;
    @Autowired
    LvPriceComboOfTourismForModifyDao lvPriceComboOfTourismForModifyDao;
    private static final Logger log = LogManager.getLogger(LvTourismOrder.class);
    private static final SimpleDateFormat DATE_FORMET = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 下单
     * @param orderInfo 订单信息
     * @param id 在售旅游产品id
     * @param customerInfos 顾客信息
     * @return
     */
    public String save(String orderInfo, String id, String customerInfos) {
        LvTourismForSale lvTourismForSale=lvTourismForSaleDao.get(id);
        if(lvTourismForSale.getStatus()==1){
            return "01";
        }
        Date now=new Date();
        JSONObject order=JSONObject.parseObject(orderInfo);
        LvTourismOrder lvTourismOrder=new LvTourismOrder();
        lvTourismOrder.setStatus(0);
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMddHH");
        Random rad = new Random();
        String radString = rad.nextInt(10000) + "";
        String orderNumber=df.format(date) + radString;
        lvTourismOrder.setOrderNumber(orderNumber);
        lvTourismOrder.setContactName(order.getString("contactName"));
        lvTourismOrder.setContactPhone(order.getString("contactPhone"));
        lvTourismOrder.setOrderTime(now);
        lvTourismOrder.setFkWalletId(order.getString("fkWalletId"));
        String userId=order.getString("fkUserId");
        lvTourismOrder.setFkUserId(userId);
        lvTourismOrder.setFkTourismModelId(order.getString("fkTourismModelId"));
        lvTourismOrder.setFkTransportId(order.getString("fkTransportId"));
        try {
            lvTourismOrder.setTourismForSaleServerDate(DATE_FORMET.parse(order.getString("serverDate")));
        }catch(Exception e){
            e.printStackTrace();
        }
        lvTourismOrder.setTourismPriceComboComment(order.getString("comment"));
        lvTourismOrder.setStartSiteName(order.getString("startSiteName"));
        lvTourismOrder.setStartSiteAddress(order.getString("startSiteAddress"));
        lvTourismOrder.setTerminalSiteName(order.getString("terminalSiteName"));
        lvTourismOrder.setTerminalSiteAddress(order.getString("terminalSiteAddress"));
        BigDecimal orderBalance=new BigDecimal(0);
        int allIncreaseGuestRoom=order.getInteger("allowIncreaseGuestRoom");
        lvTourismOrder.setAllowIncreaseGuestRoom(allIncreaseGuestRoom);
        if(allIncreaseGuestRoom==1){
            int guestRoomQuantity=order.getInteger("guestRoomQuantity");
            String fkTourismModelId=order.getString("fkTourismModelId");
            LvTourismModel lvTourismModel=lvTourismModelDao.get(fkTourismModelId);
            if(lvTourismModel.getAllowIncreaseGuestRoom()==0){
                return "04";
            }
            BigDecimal guestRoomPrice=lvTourismModel.getGuestRoomPrice();
            lvTourismOrder.setGuestRoomPrice(guestRoomPrice);
            lvTourismOrder.setGuestRoomQuantity(guestRoomQuantity);
            orderBalance=orderBalance.add(guestRoomPrice.multiply(new BigDecimal(guestRoomQuantity)));
        }
        int adultNumber=0;
        int childNumber=0;
        List<LvTourismOrderCustomer> lvTourismOrderCustomerList=new ArrayList<LvTourismOrderCustomer>();
        JSONArray customerArray=JSONArray.parseArray(customerInfos);
        for(int i=0;i<customerArray.size();i++){
           JSONObject customerObject=customerArray.getJSONObject(i);
           LvTourismOrderCustomer customer=new LvTourismOrderCustomer();
           customer.setPassengerName(customerObject.getString("passengerName"));
           String birthDayString=customerObject.getString("passengerBirthDay");
           if(birthDayString!=null&&!"".equals(birthDayString)){
               try{
                   customer.setPassengerBirthDay(DATE_FORMET.parse(birthDayString));
               }catch (Exception e){
                   e.printStackTrace();
               }
           }
           int type=customerObject.getInteger("passengerType");
           if(type==0){
               adultNumber++;
           }else if(type==1){
               childNumber++;
           }
           customer.setPassengerType(type);
           customer.setPassengerCardType(customerObject.getInteger("passengerCardType"));
           customer.setPassengerCardNumber(customerObject.getString("passengerCardNumber"));
           customer.setAddTime(now);
           customer.setFkAddUserId(userId);
           customer.setStatus(0);
           lvTourismOrderCustomerList.add(customer);
        }
        if(lvTourismForSale.getAdultSeatNumber()-lvTourismForSale.getSaledAdultSeatNumber()<adultNumber){
            return "01";
        }
        if(lvTourismForSale.getChildSeatNumber()-lvTourismForSale.getSaledChildSeatNumber()<childNumber){
            return "02";
        }
        String priceComboId=order.getString("priceComboId");
        if(lvTourismForSale.getIsModifyPriceCombo()==0){
            LvPriceComboOfTourismModel lvPriceComboOfTourismModel=lvPriceComboOfTourismModelDao.get(priceComboId);
            orderBalance=orderBalance.add(lvPriceComboOfTourismModel.getAdultPrice().multiply(new BigDecimal(adultNumber)));
            orderBalance=orderBalance.add(lvPriceComboOfTourismModel.getChildPrice().multiply(new BigDecimal(childNumber)));
        }else{
            LvPriceComboOfTourismForModify lvPriceComboOfTourismForModify=lvPriceComboOfTourismForModifyDao.get(priceComboId);
            orderBalance=orderBalance.add(lvPriceComboOfTourismForModify.getAdultPrice().multiply(new BigDecimal(adultNumber)));
            orderBalance=orderBalance.add(lvPriceComboOfTourismForModify.getChildPrice().multiply(new BigDecimal(childNumber)));
        }
        lvTourismOrder.setOrderBalance(orderBalance);
        lvTourismOrderDao.save(lvTourismOrder);
        for(int i=0;i<lvTourismOrderCustomerList.size();i++){
            LvTourismOrderCustomer lvTourismOrderCustomer=lvTourismOrderCustomerList.get(i);
            lvTourismOrderCustomer.setFkTourismOrderId(lvTourismOrder.getId());
            String result=lvTourismOrderCustomerDao.update(lvTourismOrderCustomer);
            if("0".equals(result)){
                return "0";
            }
        }
        int saledAdultSeatNumber=lvTourismForSale.getSaledAdultSeatNumber();
        int saledChildSeatNumber=lvTourismForSale.getSaledChildSeatNumber();
        if(lvTourismForSale.getAdultSeatNumber()-saledAdultSeatNumber==lvTourismForSale.getChildSeatNumber()-saledAdultSeatNumber){
            if(childNumber==0){
                lvTourismForSale.setSaledChildSeatNumber(saledChildSeatNumber+1);
            }else{
                lvTourismForSale.setSaledChildSeatNumber(saledChildSeatNumber+childNumber);
            }
        }else{
            lvTourismForSale.setSaledChildSeatNumber(saledChildSeatNumber+childNumber);
        }
        lvTourismForSale.setSaledAdultSeatNumber(saledAdultSeatNumber+adultNumber);
        if(lvTourismForSale.getAdultSeatNumber()==lvTourismForSale.getSaledAdultSeatNumber()&&lvTourismForSale.getChildSeatNumber()==lvTourismForSale.getSaledChildSeatNumber()){
            lvTourismForSale.setStatus(1);
        }
        return lvTourismForSaleDao.update(lvTourismForSale);
    }
    /**
     * 取消
     * @param id 旅游订单id
     * @param refundReason 退单理由
     * @return
     */
    public String cancel(String id,String refundReason) {
        Date now=new Date();
        LvTourismOrder lvTourismOrder=lvTourismOrderDao.get(id);
        List<LvTourismOrderCustomer> lvTourismOrderCustomerList=lvTourismOrderCustomerDao.findBy("fkTourismOrderId",lvTourismOrder.getId());
        LvTourismForSale lvTourismForSale=lvTourismForSaleManager.searchByDate(lvTourismOrder.getFkTourismModelId(),lvTourismOrder.getTourismForSaleServerDate());
        if(lvTourismOrder.getStatus()!=0&&lvTourismOrder.getStatus()!=1&&lvTourismOrder.getStatus()!=2){
            return "01";
        }
        lvTourismOrder.setRefundReason(refundReason);
        lvTourismOrder.setRefundTime(now);
        if(lvTourismOrder.getStatus()==0){
            lvTourismOrder.setStatus(-1);
            String result= lvTourismOrderDao.update(lvTourismOrder);
            if("0".equals(result)){
                return "0";
            }
        }
        if(lvTourismOrder.getStatus()==1||lvTourismOrder.getStatus()==2){
            if(lvTourismOrder.getPayMode()==6){
                ZtcWallet ztcWallet=ztcWalletDao.get(lvTourismOrder.getId());
                ztcWallet.setMoney(ztcWallet.getMoney().add(lvTourismOrder.getPayBalance()));
                ztcWallet.setTmoney(ztcWallet.getTmoney().add(lvTourismOrder.getPayBalance()));
                ztcWalletDao.update(ztcWallet);
                ZtcWalletLog ztcWalletLog=new ZtcWalletLog();
                ztcWalletLog.setFkUserId(lvTourismOrder.getFkUserId());
                ztcWalletLog.setType(2);
                ztcWalletLog.setMoney(lvTourismOrder.getPayBalance());
                ztcWalletLog.setAfterBalance(ztcWallet.getMoney());
                ztcWalletLog.setAddTime(now.getTime());
                ztcWalletLog.setComment("退款");
                ztcWalletLog.setFkOrderId(lvTourismOrder.getId());
                ztcWalletLogDao.save(ztcWalletLog);
            }else{
                LvRefundRecord lvRefundRecord=new LvRefundRecord();
                /*if(lvTourismOrder.getPayMode()==4||lvTourismOrder.getPayMode()==5){
                    String refundNo = OrderUtil.getOrderNumber("ATK");
                    boolean isOk = Alipay.refundAliPay(lvTourismOrder.getOrderNumber(), lvTourismOrder.getPayBalance().toString(), refundNo, "取消订单退款");
                    if (!isOk) {
                        throw new ServiceException(ResultEnum.ERROR_ORDER_CANCEL_FAILURE);
                    }
                    lvRefundRecord.setAliRefundNumber(refundNo);
                }else{
                    RefundParameter refundParameter = new RefundParameter();
                    refundParameter.setOutTradeNo(lvTourismOrder.getOrderNumber());
                    refundNo = OrderUtil.getOrderNumber("WTK");
                    refundParameter.setOutRefundNo(refundNo);
                    refundParameter.setRefundDesc("取消订单退款");
                    refundParameter.setTotalFee(lvTourismOrder.getPayBalance());
                    refundParameter.setRefundFee(lvTourismOrder.getPayBalance());
                    boolean isTrue;
                    try {
                        isTrue = WeChatPay.refundApply(refundParameter,lvTourismOrder.getPayMode());
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                        throw new ServiceException(ResultEnum.ERROR_ORDER_CANCEL_FAILURE);
                    }
                    if (!isTrue) {
                        throw new ServiceException(ResultEnum.ERROR_ORDER_CANCEL_FAILURE);
                    }
                    lvRefundRecord.setWxRefundNumber(refundNo);
                }*/
                lvRefundRecord.setFkManagerId(lvTourismOrder.getFkUserId());
                lvRefundRecord.setType("2");
                lvRefundRecord.setMoney(lvTourismOrder.getPayBalance());
                lvRefundRecord.setFkLvOrderId(id);
                lvRefundRecord.setAddTime(now);
                lvRefundRecordDao.save(lvRefundRecord);
                lvTourismOrder.setRefundBalance(lvTourismOrder.getPayBalance());
                lvTourismOrder.setStatus(5);
                String result= lvTourismOrderDao.update(lvTourismOrder);
                if("0".equals(result)){
                    return "0";
                }
            }
        }
        int adultNumber=0;
        int childNumber=0;
        for(int i=0;i<lvTourismOrderCustomerList.size();i++){
            LvTourismOrderCustomer lvTourismOrderCustomer=lvTourismOrderCustomerList.get(i);
            if(lvTourismOrderCustomer.getPassengerType()==0){
                adultNumber++;
            }else{
                childNumber++;
            }
            lvTourismOrderCustomer.setStatus(1);
            String result=lvTourismOrderCustomerDao.update(lvTourismOrderCustomer);
            if("0".equals(result)){
                return "0";
            }
        }
        lvTourismForSale.setSaledAdultSeatNumber(lvTourismForSale.getSaledAdultSeatNumber()-adultNumber);
        lvTourismForSale.setSaledChildSeatNumber(lvTourismForSale.getSaledChildSeatNumber()-childNumber);
        if(lvTourismForSale.getSaledChildSeatNumber()==0){
            lvTourismForSale.setSaledChildSeatNumber(0);
            lvTourismForSale.setStatus(0);
        }
        String result=lvTourismForSaleDao.update(lvTourismForSale);
        if("0".equals(result)){
            return "0";
        }
        return "1";
    }
    /**
     * 获取订单信息
     * @param id 旅游订单id
     * @return
     */
    public LvTourismOrder get(String id) {
        LvTourismOrder lvTourismOrder=lvTourismOrderDao.get(id);
        return lvTourismOrder;
    }
    /**
     * 获取订单详情
     */
    public HashMap<String, Object> getOrderDetail(String orderId) {
        List<Object[]> result = lvTourismOrderDao.getOrderDetail(orderId);
        HashMap<String, Object> map = new HashMap<>();
        if (result.size() == 0) {
            return JsonWrapper.failureWrapperMsg("获取订单信息失败！");
        }
        Object[] objects = result.get(0);
        if (objects[0] == null) {
            return JsonWrapper.failureWrapperMsg("获取订单信息失败！");
        }
        map.put("fullName", objects[0]); // 发布公司名
        map.put("routeName", objects[1]); // 线路名
        map.put("orderNumber", objects[2]); // 订单编号
        map.put("orderStatus", objects[3]); // 订单状态
        map.put("price", objects[4]); // 订单价格
        map.put("payPrice", objects[5]); // 实际支付
        map.put("refundPrice", objects[6]); // 退款金额（退款）
        map.put("contactName", objects[7]); // 联系人
        map.put("contactTel", objects[8]); // 联系人手机
        map.put("orderType", objects[9]); // 类型：0.车票，1.充值
        map.put("payType", objects[10]); // 支付类型：1.余额2.支付宝3.微信
        map.put("orderTime", objects[11]); // 下单时间
        map.put("payTime", objects[12]); // 支付时间
        map.put("fullTime", objects[13]); // 完成时间
        map.put("refundTime", objects[14]); // 退款时间（退款）
        map.put("refundRemark", objects[15]);// 退款说明
        String[] passengers = objects[16].toString().split(";"); // 乘客
        List<HashMap<String, Object>> resultPerson = new ArrayList<>();
        if (passengers.length > 0) {
            HashMap<String, Object> map1 = null;
            for (String temp : passengers) {
                map1 = new HashMap<>();
                String[] tempPerson = temp.split(",");
                map1.put("passengerName", tempPerson[0]); // 乘客姓名
                map1.put("passengerBirthday", tempPerson[1]); // 乘客生日
                map1.put("passengerType", tempPerson[2]); // 乘客类型
                map1.put("passengerCardNumber", tempPerson[3]);// 乘客证件号码
                map1.put("passengerCardType", tempPerson[4]); // 乘客证件类型
                resultPerson.add(map1);
            }
        }
        map.put("person", resultPerson);
        map.put("id", objects[17]);
        return JsonWrapper.successWrapper(map);
    }
   /* public List<LvTourismOrder> getList(String userId, String transportId) {
        if(userId!=null&&!"".equals(userId)){
            List<LvTourismOrder> lvTourismOrderList=lvTourismOrderDao.findBy("fkUserId",userId);
            return  lvTourismOrderList;
        }else if(transportId!=null&&!"".equals(transportId)){
            List<LvTourismOrder> lvTourismOrderList=lvTourismOrderDao.findBy("fkTransportId",transportId);
            return  lvTourismOrderList;
        }
        return null;
    }*/

    /**
     * 设置过期订单（下单后5分钟内未支付）为取消状态
     * @return
     */
    public String setOrderCancelStatus(){
        String sql = "select * from lv_tourism_order where"
                +" lv_tourism_order.status='0'"
                +" and date_add(lv_tourism_order.order_time, interval 5 minute)>now()"
                +" order by lv_tourism_order.order_time";
        List<LvTourismOrder> list=lvTourismOrderDao.search(sql);
       if(list!=null&&list.size()>0){
           for(int i=0;i<list.size();i++){
               LvTourismOrder lvTourismOrder=lvTourismOrderDao.get(i);
               Date now=new Date();
               List<LvTourismOrderCustomer> lvTourismOrderCustomerList=lvTourismOrderCustomerDao.findBy("fkTourismOrderId",lvTourismOrder.getId());
               LvTourismForSale lvTourismForSale=lvTourismForSaleManager.searchByDate(lvTourismOrder.getFkTourismModelId(),lvTourismOrder.getTourismForSaleServerDate());
               lvTourismOrder.setRefundTime(now);
               lvTourismOrder.setStatus(-1);
               String result= lvTourismOrderDao.update(lvTourismOrder);
               if("0".equals(result)){
                   return "0";
               }
               int adultNumber=0;
               int childNumber=0;
               for(int j=0;j<lvTourismOrderCustomerList.size();j++){
                   LvTourismOrderCustomer lvTourismOrderCustomer=lvTourismOrderCustomerList.get(j);
                   if(lvTourismOrderCustomer.getPassengerType()==0){
                       adultNumber++;
                   }else{
                       childNumber++;
                   }
                   lvTourismOrderCustomer.setStatus(2);
                   result=lvTourismOrderCustomerDao.update(lvTourismOrderCustomer);
                   if("0".equals(result)){
                       return "0";
                   }
               }
               lvTourismForSale.setSaledAdultSeatNumber(lvTourismForSale.getSaledAdultSeatNumber()-adultNumber);
               lvTourismForSale.setSaledChildSeatNumber(lvTourismForSale.getSaledChildSeatNumber()-childNumber);
               if(lvTourismForSale.getSaledChildSeatNumber()==0){
                   lvTourismForSale.setSaledChildSeatNumber(0);
                   lvTourismForSale.setStatus(0);
               }
               result=lvTourismForSaleDao.update(lvTourismForSale);
               if("0".equals(result)){
                   return "0";
               }
           }
       }
        return "1";
    }
    /**
     * 获得当前公司所有订单（旅游）
     * @param pageSize  分页大小
     * @param pageNumber  页码
     * @param orderId    订单编号（搜索）
     * @param departDateStart   出发日期（搜索）
     * @param departDateEnd   出发日期（搜索）
     * @param contactName   联系人（搜索）
     * @param contactPhone  联系电话（搜索）
     * @param startDate  开始时间（搜索）
     * @param endDate    结束时间（搜索）
     * @param orderStatus    订单状态（搜索）
     * @return  	符合条件的所有订单
     */
    public Pager<Map> getList(ZtcTransport ztcTransport, Integer pageSize, Integer pageNumber, String orderId, String startDate, String endDate, String departDateStart, String departDateEnd, String contactName, String contactPhone, String orderStatus) {
        return lvTourismForSaleDao.getList(ztcTransport, pageSize, pageNumber, orderId, startDate,endDate,departDateStart,departDateEnd,contactName,contactPhone,orderStatus);
    }
    /**
     * 导出所有订单 （直通车）
     */
    public List<Map> getList(ZtcTransport ztcTransport,String orderId, String startDate, String endDate,String departDateStart,String departDateEnd,String contactName,String contactPhone,String orderStatus) {
        return lvTourismForSaleDao.getList(ztcTransport, orderId, startDate,endDate,departDateStart,departDateEnd,contactName,contactPhone,orderStatus);
    }
   /* public List<LvTourismOrder> getList(String pageNumber,String pageSize,String orderNumber,String tourismName,String status,String transportId) {
        String sql = "select lv_tourism_order.* from lv_tourism_order";
        int startIndex = 0;
        int endIndex = Integer.parseInt(pageSize);
        if (tourismName != null&& !"".equals(tourismName)) {
            sql += ",lv_tourism_model where lv_tourism_model.id=lv_tourism_order.fk_tourism_model_id and lv_tourism_model.tourism_name like '%" + tourismName + "%'";
        }else{
            sql += " where 1=1";
        }
        if (orderNumber != null&& !"".equals(orderNumber)) {
            sql += " and order_number='" + orderNumber + "'";
        }
        if(status!=null&&!"".equals(status)){
            String[] statusArray=status.split(",");
            String statusSql="(";
            for(int i=0;i<statusArray.length;i++){
                if(i==0){
                    statusSql+=" status='"+statusArray[i]+"'";
                }else{
                    statusSql+=" or status='"+statusArray[i]+"'";
                }
            }
            statusSql+=")";
            if(!"()".equals(statusSql)){
                sql+=" and "+statusSql;
            }
        }
        sql += " order by case when order_time is null then pay_time " +
                "when pay_time is null then finish_time " +
                "when finish_time is null then refund_time " +
                "else handle_time end desc";
        if (pageNumber == null) {
            pageNumber = "0";
        } else {
            int number = Integer.parseInt(pageNumber);
            if (!"0".equals(pageNumber)) {
                number = number - 1;
            }
            startIndex = (number) * Integer.parseInt(pageSize);
        }
        sql += " limit " + startIndex + "," + endIndex;
        List<LvTourismOrder> list=lvTourismOrderDao.search(sql);
        return list;
    }
    public int getCount(String orderNumber, String tourismName, String status, String transportId) {
        String sql = "select lv_tourism_order.* from lv_tourism_order";
        if (tourismName != null&& !"".equals(tourismName)) {
            sql += ",lv_tourism_model where lv_tourism_model.id=lv_tourism_order.fk_tourism_model_id and lv_tourism_model.tourism_name like '%" + tourismName + "%'";
        }else{
            sql += " where 1=1";
        }
        if (orderNumber != null&& !"".equals(orderNumber)) {
            sql += " and order_number='" + orderNumber + "'";
        }
        if(status!=null&&!"".equals(status)){
            String[] statusArray=status.split(",");
            String statusSql="(";
            for(int i=0;i<statusArray.length;i++){
                if(i==0){
                    statusSql+=" status='"+statusArray[i]+"'";
                }else{
                    statusSql+=" or status='"+statusArray[i]+"'";
                }
            }
            statusSql+=")";
            if(!"()".equals(statusSql)){
                sql+=" and "+statusSql;
            }
        }
        sql += " order by case when order_time is null then pay_time " +
                "when pay_time is null then finish_time " +
                "when finish_time is null then refund_time " +
                "else handle_time end desc";
        int count = lvTourismModelDao.getSqlSumQuery(sql);
        return count;
    }*/
}
